<?php
/**
 * 侧栏模块设置
 * $Id: sidebar_module.php 238 2011-01-13 08:53:47Z bwanglei@gmail.com $
 */

require_once './globals.php';
require_once TEMPLATE_PATH.'/each_module.php';

$widgets = !empty($options_cache['widgets']) ? unserialize($options_cache['widgets']) : array();
$widget_title = @unserialize($options_cache['widget_title']);

//ajax请求改变模块顺序
if ($_SERVER['REQUEST_METHOD']=='POST' && $action=='neworderlist') {
	$new_order = $_POST['new_order'];
	$new_order = preg_split('/,/',$new_order);	//字符串转化为数组
	$new_widgets = array();
	foreach ($new_order as $order) {
		$order_t = del_b($order);
		if (isset($widget_title[$order_t])) $new_widgets[$order] = $widget_title[$order_t];
	}
	//序列化新生成的数组并保存在数据库中
	$new_widgets = serialize($new_widgets);
	$update_widgets_sql = 'UPDATE `'.DB_PREFIX.'settings` SET `value`=\''.$new_widgets.'\' WHERE `name`=\'widgets\'';
	$DB->query($update_widgets_sql);
	$CACHE->updateCache('options');
	echo '更新完毕';exit;
}

//ajax 添加模块
if ($_SERVER['REQUEST_METHOD']=='POST' && $action=='addorderlist') {
	include_once TEMPLATE_PATH.'/each_module.php';
	$add_m_name = $_POST['add_name'];
	$new_w_t = array();
	if (isset($widget_title[$add_m_name])) {
		$add_m_info = $widget_title[$add_m_name];
		while (isset($widgets[$add_m_name])) {
			$add_m_name .= '_';
		}
	} else {
		echo '';exit;
	}
	$widgets[$add_m_name] = $add_m_info;
	//存入数据库
	$widgets = serialize($widgets);
	$update_widgets = 'UPDATE `'.DB_PREFIX.'settings` SET `value`=\''.$widgets.'\' WHERE `name`=\'widgets\'';
	$DB->query($update_widgets);
	$CACHE->updateCache('options');
	call_user_func('e_'.del_b($add_m_name), $add_m_name, $add_m_info);exit;
}

//ajax删除模块
if ($_SERVER['REQUEST_METHOD']=='POST' && $action == 'delorderlist') {
	$del_m_name = $_POST['del_name'];
	if (isset($widgets[$del_m_name])) {
		unset($widgets[$del_m_name]);
	} else {
		echo 'false';exit;
	}
	//存入数据库
	$widgets = serialize($widgets);
	$update_widgets = 'UPDATE `'.DB_PREFIX.'settings` SET `value`=\''.$widgets.'\' WHERE `name`=\'widgets\'';
	$DB->query($update_widgets);
	$CACHE->updateCache('options');
	echo 'true';exit;
}

//ajax请求更新模块具体信息
if ($_SERVER['REQUEST_METHOD']=='POST' && $action=='uinfo') {
	$m_type = $_POST['m_type'];
	unset($_POST['m_type']);
	foreach ($_POST as $name=>$value) {
		$widgets[$m_type][$name] = $value;
	}
	$widgets = serialize($widgets);
	$u_query = 'UPDATE `'.DB_PREFIX.'settings` SET `value`=\''.$widgets.'\' WHERE `name`=\'widgets\'';
	$DB->query($u_query);
	$CACHE->updateCache('options');echo 'true';exit;
}
include_once TEMPLATE_PATH.'/header.php';
include_once TEMPLATE_PATH.'/sidebar_module.php';
include_once TEMPLATE_PATH.'/sidebar.php';
include_once TEMPLATE_PATH.'/footer.php';
